package com.untour.im.client;

import com.untour.im.client.handler.*;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import org.springframework.util.Assert;

public class ClientHandlersInitializer extends ChannelInitializer<SocketChannel> {

    private TcpClient tcpClient;

    public ClientHandlersInitializer(TcpClient tcpClient) {
        Assert.notNull(tcpClient, "TcpClient can not be null.");
        this.tcpClient = tcpClient;
    }

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(new ClientIdleStateHandler());
        pipeline.addLast(new ClientChannelDuplexHandler());
        pipeline.addLast(new ReconnectHandler(tcpClient));
        pipeline.addLast(new ServerResponseMessageDecoderHandler());
        pipeline.addLast(new ServerResponseMessageHandler());
        pipeline.addLast(new OfflineServerResponseMessageHandler(tcpClient));
        pipeline.addLast(new RequestMessageEncoderHandler(tcpClient.getClientType()));
//        pipeline.addLast(new ClientHeartBeatHandler(tcpClient));
    }
}

